package com.wave.wavesystem.server.controller;

import com.mybatisflex.core.paginate.Page;
import com.wave.wavesystem.business.annotation.AuthCheck;
import com.wave.wavesystem.business.pojo.emums.user.UserRoleEnum;
import com.wave.wavesystem.server.pojo.dto.ArticleQueryRequest;
import com.wave.wavesystem.server.pojo.vo.article.ArticleHomeCardVO;
import com.wave.wavesystem.server.pojo.vo.article.ArticleServerTableVO;
import wave.common.pojo.enums.RStatusEnum;
import wave.common.pojo.entity.Result;
import com.wave.wavesystem.common.util.Condition;
import com.wave.wavesystem.common.util.Func;
import com.wave.wavesystem.server.pojo.entity.ArticleEntity;
import com.wave.wavesystem.server.service.ArticleService;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;

import java.util.List;

/**
 * @author 夕下风波
 * @date 2025-02-23
 * @description 文章表 访问层
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    /**
     * 新增
     *
     * @param article 新增实体
     */
    @Operation(summary = "保存")
    @PostMapping("/save")
    public Result<ArticleEntity> save(@RequestBody ArticleEntity article) {
        return Result.data(articleService.mySave(article));
    }

    /**
     * 删除/批量删除
     *
     * @param ids id集合
     */
    @Operation(summary = "删除")
    @PostMapping("/remove")
    public Result<Boolean> remove(String ids) {
        List<String> idList = Func.strToStringList(ids);
        return Result.status(articleService.removeByIds(idList), RStatusEnum.SUCCESS);
    }

    /**
     * 更新
     *
     * @param article 更新内容
     */
    @Operation(summary = "更新")
    @PostMapping("/update")
    public Result<ArticleEntity> update(@RequestBody ArticleEntity article) {
        articleService.updateById(article);
        return Result.data(articleService.getById(article.getId()), RStatusEnum.SUCCESS);
    }

    /**
     * 详情查询
     *
     * @param article 查询条件
     */
    @Operation(summary = "详情")
    @GetMapping("/detail")
    public Result<ArticleEntity> detail(ArticleQueryRequest article) {
        return Result.data(articleService.getById(article.getId()));
    }

    /**
     * 列表查询
     *
     * @param articleQueryRequest 查询条件
     */
    @Operation(summary = "列表")
    @GetMapping("/list")
    public Result<List<ArticleEntity>> list(ArticleQueryRequest articleQueryRequest) {
        ArticleEntity article = new ArticleEntity();
        Func.beanCopy(articleQueryRequest, article);
        return Result.data(articleService.list(Condition.getQueryWrapper(article)));
    }

    /**
     * 条件分页查询
     *
     * @param articleQueryRequest 查询条件
     */
    @Operation(summary = "分页")
    @GetMapping("/page")
    public Result<Page<ArticleEntity>> page(ArticleQueryRequest articleQueryRequest) {
        ArticleEntity article = new ArticleEntity();
        Func.beanCopy(articleQueryRequest, article);
        return Result.data(articleService.page(Condition.getPage(articleQueryRequest),
                Condition.getQueryWrapper(article)));
    }

    /**
     * 条件分页查询
     *
     * @param articleQueryRequest 查询条件
     */
    @Operation(summary = "分页")
    @PostMapping("/admin/page")
    public Result<Page<ArticleServerTableVO>> adminPage(@RequestBody ArticleQueryRequest articleQueryRequest) {
        ArticleEntity article = new ArticleEntity();
        Func.beanCopy(articleQueryRequest, article);
        return Result.data(articleService.adminPage(Condition.getPage(articleQueryRequest),
                Condition.getQueryWrapper(article)));
    }

    /**
     * 首页获取文章卡片 page
     *
     * @param articleQueryRequest 分页
     * @return 分页数据
     */
    @Operation(summary = "首页获取文章卡片 page")
    @PostMapping("/home/card/page")
    public Result<Page<ArticleHomeCardVO>> getHomeCardPage(@RequestBody ArticleQueryRequest articleQueryRequest) {
        return Result.data(articleService.getHomeCardPage(articleQueryRequest));
    }

}